﻿var PLAYER_IDLE = 0;
var PLAYER_PLAYING = 1;
var PLAYER_PAUSED = 2;
var PLAYER_STOPPING = 3;

var MODE_DVD = 1;
var MODE_VCD = 2;
var MODE_SVCD = 3;
var MODE_CD = 4;
var MODE_UNKNOWN = 5;

var PLAYER_POS = 0;
var PLAYER_STATE = 1;
var PLAYER_PERCENTAGE = 2;
var PLAYER_DURATION = 3;
var PLAYER_HAS_AUDIO = 4;
var PLAYER_HAS_VIDEO = 5;


var stopped = true;
var seeking = false;
var paused = false;
var playingFile;

function initPlayer()
{
    player.init();
}
    
function uninit()
{
    if(stopped) player.stop();
    stopped = true;
    player.uninit();
}

function startPlay(fName)
{
        if (isPlaying()) {
            player.stop();
        }
        stopped = false
        if(fName) playingFile = fName;
        
        //bDetached is a checkbox on html form. To determine whether the video is detached form the embed.
        var detached = false;//$("bDetached").checked == true ? true : false;
        if (!playingFile) return;
        var opts = " -framedrop -autosync 30 -vf screenshot";
        player.play(playingFile, opts, !detached);
}

function getPlayingFile()
{
	return playingFile;
}

function setPlayingFile(fName)
{
	playingFile = fName;
}

function isPlaying()
{
	return player.getInfo(PLAYER_STATE, 0) == PLAYER_PLAYING
}
function openFile()
{
        var fname = loadFile("Play File");
        if (fname) {
        	 playingFile = fname;
        }
}
function openUrl()
{
        var tips = "Xulplayer Support following protocol: http, rtsp, mms";
        var urls = prompt(tips, "Playe URL", "");
        if (urls) {
        		playingFile = urls;
            startPlay();
        }
}

function pause_cmd()
{
        if (paused) paused = false;
        player.pause();
}

function play_cmd()
{
        if (playingFile)
                startPlay();
        else
                openFile();
}

function stop_cmd()
{
        stopped = true;
        player.stop();
}

function seekToPos(pos)
{
        player.seek(pos, 2);
        seeking = false;
}

function switchAudio()
{
        var ret = player.sendCommand("switch_audio", true);
}

function applyPlayerOptions()
{
      if (isPlaying()) {
              seeking = true;
              var pos = player.getInfo(PLAYER_POS);
              player.stop();
              startPlay();
              player.seek(pos, 2)        
              seeking = false;
      }
}

function sendPlayerCmd(cmd)
{
        if (isPlaying()) {
            player.sendCommand(cmd);
        }
}

function QueryPlayer(cmd)
{
      if (isPlaying()) {
          var result = player.sendCommand(cmd, isChecked("optHasResult"));
          if (result) setValue("cmdResult", result);
      }
}

function takeScreenshot()
{
      if (player.getInfo(PLAYER_HAS_VIDEO)) {
              var result = player.sendCommand("screenshot 0", true);
              if(!result) return;
               alert(result);
      }
}